﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TextRPG.Models;
using TextRPG.Models.ViewModels;

namespace TextRPG.Controllers
{ 
    public class InventoryController : Controller
    {
        private RPGContext db = new RPGContext();

        //
        // GET: /Inventory/

        public ViewResult Index()
        {
            return View(db.Inventorys.ToList());
        }

        //
        // GET: /Inventory/Details/5

        public ViewResult Details(int id)
        {
            Inventory inventory = db.Inventorys.Find(id);

            List<ItemInventoryRelation> iir = db.ItemInventoryRelations.Where(x => x.InventoryID == id).ToList();
            foreach (ItemInventoryRelation i in iir)
            {
                Item item = db.Items.Where(x => x.ItemID == i.ItemID).Single();
                inventory.Items.Add(item);
            }

            return View(inventory);
        }

        //
        // GET: /Character/AddItem/5

        public ActionResult AddItem(int id)
        {
            ItemInventoryRelation iir = new ItemInventoryRelation();
            iir.InventoryID = id;
            PopulateItemList();
            return View(iir);
        }

        //
        // POST: /Character/AddItem/5

        [HttpPost]
        public ActionResult AddItem(ItemInventoryRelation addItem)
        {
            if (ModelState.IsValid)
            {
                db.ItemInventoryRelations.Add(addItem);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

           // ViewBag.InventoryID = new SelectList(db.Inventorys, "InventoryID", "InventoryID", character.InventoryID);
            return View(addItem);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private void PopulateItemList()
        {
            var departmentsQuery = from d in db.Items
                                   orderby d.Name
                                   select d;
            ViewBag.ItemID = new SelectList(departmentsQuery, "ItemID", "Name");
        }
    }
}